package day10;

import day9.Account;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @author oldliu
 * @since 1.0
 */
public class CreateAccount {
    public static void main(String[] args) {
        Class<Account> accountClass = Account.class;
        try {
            Constructor<Account> ac = accountClass.getDeclaredConstructor(String.class, String.class, float.class);
            ac.setAccessible(true);//突破private限制
            Account account = ac.newInstance("cn---1", "老刘", 10000.0F);
            System.out.println(account);

            Field fmoney=accountClass.getDeclaredField("balance");
            fmoney.setAccessible(true);
            fmoney.setFloat(account,400000F);
            System.out.println(account);
            //setNo
            Method mno=accountClass.getDeclaredMethod("setNo",String.class);
            Object rtn= mno.invoke(account,"icbc00089");
            System.out.println(rtn);
            System.out.println("调用setNo后：");
            System.out.println(account);
        } catch (Exception e) {
            e.printStackTrace();
        }
//        try {
//            //调用无参的构造方法
//            Account account = accountClass.newInstance();
//            System.out.println(account);
//        } catch (InstantiationException e) {
//            e.printStackTrace();
//        } catch (IllegalAccessException e) {
//            e.printStackTrace();
//        }
    }
}
